﻿using System.Reflection;
using Autofac;
using MG.SmartHMI.ViewModel;
using MG.SmartHMI.ViewModel.Pages.Setting;

namespace MG.SmartHMI
{
    public class ModelLocator
    {
        private static IContainer Service { get; set; }
        private static ContainerBuilder Bulider { get; set; }

        /// <summary>
        /// 注入Model
        /// </summary>
        /// <returns></returns>
        public static IContainer ModelInjection()
        {
            //单例生命周期                SingleInstance()
            //瞬时生命周期                InstancePerDependency()
            //作用域生命周期              InstancePerLifetimeScope()
            //作用域范围生命周期          InstancePerMatchingLifetimeScope()

            if (Service != null) return Service;

            Bulider = new ContainerBuilder();

            /*---------------------注入ViewModel程序集 ------------------------------------*/

            var asseViewModel = Assembly.Load(App.Current.GetType().Namespace);
            Bulider.RegisterAssemblyTypes(asseViewModel).Where(t => t.Name.EndsWith("ViewModel")).SingleInstance();

            /*---------------------注入View程序集 ------------------------------------*/
            Bulider.RegisterAssemblyTypes(asseViewModel).Where(t => t.Name.EndsWith("View")).SingleInstance();

            Service = Bulider.Build();
            return Service;
        }

        public static LayoutViewModel Layout => Service.Resolve<LayoutViewModel>();

        public static MainViewModel Main => Service.Resolve<MainViewModel>();

        public static HomeViewModel Home => Service.Resolve<HomeViewModel>();

        public static DebugViewModel Debug => Service.Resolve<DebugViewModel>();

        public static SlnViewModel Sln => Service.Resolve<SlnViewModel>();

        public static SettingViewModel Setting => Service.Resolve<SettingViewModel>();

        public static AxisViewModel Axis => Service.Resolve<AxisViewModel>();
        public static MessageStateViewModel MessageState => Service.Resolve<MessageStateViewModel>();
        public static ConfigViewModel Communication => Service.Resolve<ConfigViewModel>();

        public static RegisterViewModel Register => Service.Resolve<RegisterViewModel>();
    }
}